﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Threading;
using AdRotator;
using Common.Library;
using Common.Utilities;
using MarkHeath.StarRating;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using OnlineVideos.Data;
using OnlineVideos.Entities;
using OnlineVideos.View_Models;

namespace OnlineMovies.Views
{
    public partial class Advertisement : PhoneApplicationPage
    {
        int elapsedtime = 0;
        int time = 0;
        ShowList MovieDetails = null;
        ShowDetail showdetail;
        DispatcherTimer dt;
        public Advertisement()
        {
            InitializeComponent();
            showdetail = new ShowDetail();
            MovieDetails = new ShowList();
           
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
            AdRotatorControl Adc = frameBorder.FindName("AdRotatorWin8") as AdRotatorControl;
            if (Adc != null)
            {
                Adc.IsAdRotatorEnabled = false;
                Adc.Visibility = Visibility.Collapsed;
            }
            //if (Constants.YoutubePlayList.Count == 0)
            //{
               
            //    if (NavigationService.CanGoBack)
            //        NavigationService.GoBack();
            //}
            //else
            //{
                elapsedtime = 15;
                time = 0;

                dt = new DispatcherTimer();
                dt.Interval = TimeSpan.FromSeconds(1);
                dt.Tick += dt_Tick;
                dt.Start();
                LoadDescriptionSection(AppSettings.ShowUniqueID);
            //}
        }
        protected override void OnBackKeyPress(CancelEventArgs e)
        {
           
            dt.Stop();
            if (ResourceHelper.AppName != Apps.Indian_Cinema_Pro.ToString() && ResourceHelper.AppName != Apps.Kids_TV_Pro.ToString() && ResourceHelper.AppName != Apps.Story_Time_Pro.ToString())
                AppSettings.startplaying = false;
            else
                AppSettings.startplayingforpro = false;
            while (NavigationService.BackStack.Count() > 1)
            {
                if (NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("Youtube") || NavigationService.BackStack.FirstOrDefault().Source.OriginalString.Contains("Advertisement"))
                {
                    NavigationService.RemoveBackEntry();

                }
                else
                    break;
            }
           
        }
        void Advertisement_Loaded(object sender, RoutedEventArgs e)
        {
            if (ResourceHelper.ProjectName.Contains("Yoga"))
                AdRotator12duplex.DefaultSettingsFileUri = new Uri("/Yoga Regimen;component/DefaultData/defaultAdSettings.xml", UriKind.RelativeOrAbsolute);
            else
                AdRotator12duplex.DefaultSettingsFileUri = new Uri("/" + ResourceHelper.ProjectName.Replace(" ", "") + ";component/DefaultData/defaultAdSettings.xml", UriKind.RelativeOrAbsolute);
            AdRotator12duplex.SettingsUrl = AppResources.AdRotatorSettingsUrl;
            AdRotator12duplex.Invalidate();
          
        }
        void dt_Tick(object sender, object e)
        {
            if (time < 15)
            {
                elapsedtimetblk.Text = "Video: " + AppSettings.PlayVideoTitle + "- Next video will resume in " + elapsedtime-- + " seconds";
                time++;
            }
            else
            {
                (sender as DispatcherTimer).Stop();
                if (Constants.YoutubePlayList != null)
                {
                    if (Constants.YoutubePlayList.Count > 0)
                    {
                        AppSettings.LinkUrl = Constants.YoutubePlayList.FirstOrDefault().Key;
                        AppSettings.LinkTitle=Constants.YoutubePlayList.FirstOrDefault().Value;
                        Constants.YoutubePlayList.Remove(Constants.YoutubePlayList.FirstOrDefault().Key);
                        AppSettings.PlayVideoTitle = Constants.YoutubePlayList.FirstOrDefault().Value;
                        NavigationService.Navigate(new Uri("/Views/Youtube.xaml?myid=" + AppSettings.LinkUrl, UriKind.Relative));

                        //YoutubeApi.Youtube.PlayYoutubeVideo(AppSettings.LinkUrl);
                    }
                }
                    //else if (Constants.YoutubePlayList.Count == 0)
                    //{
                    //    if (NavigationService.CanGoBack)
                    //        NavigationService.GoBack();
                    //}
                }
            }

        
        private void LoadDescriptionSection(long showID)
        {
            try
            {
                BackgroundHelper bwHelper = new BackgroundHelper();

                bwHelper.AddBackgroundTask(
                                            (object s, DoWorkEventArgs a) =>
                                            {

                                                a.Result = OnlineShow.GetShowDetail(showID);
                                            },
                                            (object s, RunWorkerCompletedEventArgs a) =>
                                            {
                                                MovieDetails = (ShowList)a.Result;
                                                if (MovieDetails != null)
                                                {
                                                    tblkTitle.Text = MovieDetails.Title;
                                                    if (MovieDetails.ReleaseDate != null)
                                                    {
                                                        if (MovieDetails.ReleaseDate.Contains(','))
                                                        {
                                                            tblRelease.Text = MovieDetails.ReleaseDate.Substring(MovieDetails.ReleaseDate.LastIndexOf(',') + 1);

                                                        }
                                                        else if (ResourceHelper.ProjectName == "Indian Cinema" || ResourceHelper.ProjectName == "Indian Cinema Pro" || ResourceHelper.ProjectName == "Bollywood Movies" || ResourceHelper.ProjectName == "Bollywood Music")
                                                        {
                                                            tblRelease.Text = MovieDetails.ReleaseDate.Substring(MovieDetails.ReleaseDate.Length - 4);
                                                        }
                                                        else
                                                        {
                                                            tblRelease.Text = MovieDetails.ReleaseDate.Replace("12:00:00 AM", "");
                                                        }
                                                    }
                                                    tblkTitle1.Text = MovieDetails.Title;
                                                    double rateindconverted = Convert.ToDouble(MovieDetails.Rating) * 2;
                                                    rate.Rating = Convert.ToInt32(rateindconverted);
                                                    imgMovie.Source = ImageHelper.LoadTileImage(MovieDetails.TileImage);
                                                }

                                            }
                                          );

                bwHelper.RunBackgroundWorkers();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in LoadDescriptionSection method In ShowDescription.cs file.", ex);
            }
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            Border frameBorder = (Border)VisualTreeHelper.GetChild(Application.Current.RootVisual, 0);
            AdRotatorControl Adc = frameBorder.FindName("AdRotatorWin8") as AdRotatorControl;
            if (Adc != null)
            {
                Adc.IsAdRotatorEnabled = true;
                Adc.Visibility = Visibility.Visible;
            }
        }
        private void rate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            StarRatingControl starratingcontrol = new StarRatingControl();
            starratingcontrol.Rating = rate.Rating;
            showdetail.RateThisShow(starratingcontrol);
        }
    }
}